package com.kanche.bigbaby.collection.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kanche.bigbaby.R;
import com.kanche.bigbaby.collection.ImageUtils;
import com.kanche.bigbaby.collection.StringUtils;
import com.kanche.bigbaby.collection.ViewUtlis;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.squareup.picasso.Picasso;

/**
 * Created by nealmi on 6/11/14.
 */
public class ItemAvatarConfig extends ItemConfig {
    private String hint;
    private int layoutId = R.layout.input_list_item_avatar;
    private int width;
    private int height;

    public ItemAvatarConfig(String key, String value, String label, String hint) {
        super(key, value, label);
        this.hint = hint;
    }

    public ItemAvatarConfig() {
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    @Override
    public View getView(View convertView, Context mContext) {
        Holder holder = new Holder();

        LayoutInflater inflater = LayoutInflater.from(mContext);

        convertView = inflater.inflate(layoutId, null);
        holder.label = (TextView) convertView.findViewById(R.id.label);
        holder.hint = (TextView) convertView.findViewById(R.id.hint);
        holder.imageView = (ImageView) convertView.findViewById(R.id.image_view);
        holder.label.setText(getLabel());
        holder.hint.setText(hint);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.imageView.getLayoutParams();
        params.width = ViewUtlis.dip2px(mContext, width == 0 ? 60 : width);
        params.height = ViewUtlis.dip2px(mContext, height == 0 ? 60 : height);
        holder.imageView.setLayoutParams(params);
        if (StringUtils.hasText(getValue())) {
            DisplayImageOptions options = new DisplayImageOptions.Builder()
//                    .considerExifParams(MerchantService.CurrentAppConfig.CONSIDER_EXIF_PARAMS)
                    .considerExifParams(true)
                    .postProcessor(new ImageUtils.RoundedImageProcessor(mContext.getResources().getDimensionPixelSize(R.dimen.profile_avatar_rounded_dp)))
                    .build();
            if (StringUtils.isHttpPath(getValue())) {

                ImageLoader.getInstance().displayImage(getValue(), holder.imageView, options);
//                Picasso.with(mContext).load(getValue())
//                        .resizeDimen(R.dimen.profile_avatar_size, R.dimen.profile_avatar_size)
//                        .transform(new ImageUtils.RoundedImageTransformation(mContext.getResources().getDimensionPixelSize(R.dimen.profile_avatar_rounded_dp)))
//                        .into(holder.imageView);
            } else {
                ImageLoader.getInstance().displayImage("file:/" + getValue(), holder.imageView, options);

//                Picasso.with(mContext).load(new File(getValue()))
//                        .resizeDimen(R.dimen.profile_avatar_size, R.dimen.profile_avatar_size)
//                        .transform(new ImageUtils.RoundedImageTransformation(mContext.getResources().getDimensionPixelSize(R.dimen.profile_avatar_rounded_dp)))
//                        .into(holder.imageView);
            }

        } else {
            Picasso.with(mContext).load(R.drawable.head_bg)
                    .transform(new ImageUtils.RoundedImageTransformation(mContext.getResources().getDimensionPixelSize(R.dimen.profile_avatar_rounded_dp)))
                    .into(holder.imageView);
        }

        return convertView;
    }

    public String getHint() {
        return hint;
    }

    public void setHint(String hint) {
        this.hint = hint;
    }


    class Holder {
        TextView label, hint;
        ImageView imageView;
    }
}
